package com.p24.controller;

import com.p24.R;
import com.p24.activities.P24MfoInfo;
import com.p24.activities.P24Options;
import com.p24.activities.P24PayUa;
import com.p24.activities.P24PayVisa;
import com.p24.constants.IntentConstants;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class P24Droid extends Activity {
	//Objects
	//--Functions Container
	private ListView functionsContainer = null;
	//--List Adapter for ListView
	private ArrayAdapter<CharSequence> functionsAdapter = null;
	//Main events
	//--On Create
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        functionsContainer = (ListView) findViewById(R.id.functionsContainer);
        functionsAdapter = ArrayAdapter.createFromResource(
        		this, R.array.P24FunctionsEn,
        		android.R.layout.simple_list_item_1);
        functionsContainer.setAdapter(functionsAdapter);
        functionsContainer.setOnItemClickListener(new functionsContainerOnClickListener());
    }
    
    //Menus
    //--Main menu
    @Override 
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}
    
    //Menu Events Listeners
    //--Main Menu item selected
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.mainMenuOptions:
        	Intent optionsActivity = new Intent(this, P24Options.class);
        	startActivityForResult(optionsActivity, IntentConstants.ACTIVITY_OPTIONS);
        	
        	return true;
        case R.id.mainMenuQuit:
            closeDialog();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    //Dialogs
    //--Close Dialog
    private void closeDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?")
		       .setCancelable(false)
		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                finish();
		           }
		       })
		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
	}
    
    //List View Events
    class functionsContainerOnClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent additionalActivity = null;
			switch(arg2) {
			case 0:
				additionalActivity = new Intent(P24Droid.this, P24PayUa.class);
	        	startActivityForResult(additionalActivity, IntentConstants.ACTIVITY_P24_PAY_UA);
	        	
				break;
			case 1:
				additionalActivity = new Intent(P24Droid.this, P24PayVisa.class);
	        	startActivityForResult(additionalActivity, IntentConstants.ACTIVITY_P24_PAY_VISA);
	        	
				break;
			case 2:
				additionalActivity = new Intent(P24Droid.this, P24MfoInfo.class);
	        	startActivityForResult(additionalActivity, IntentConstants.ACTIVITY_P24_MFO_INFO);
	        	
				break;
			}
		}	
    }
}